package com.zygh.lz.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zygh.lz.entity.duty.DutySchedule;
import com.zygh.lz.service.DutyScheduleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lee
 */
@Component
public class SignUtil {
    @Resource
    private DutyScheduleService dutyScheduleService;
    /**
     * 是否在签到时间范围
     * @param modeCode 模式
     * @return boolean
     */
    public boolean signIn(Integer modeCode){
        List<DutySchedule> dutySchedules = dutyScheduleService.findOneByModeCode(modeCode);
        for(DutySchedule dutySchedule:dutySchedules){
            boolean isSign = DateUtil.isIn(DateUtil.date(), DateUtil.parse(dutySchedule.getSignInStart()), DateUtil.parse(dutySchedule.getSignInEnd()));
            if(isSign){
                return true;
            }
        }
        return false;
    }



    /**
     * 是否在签退时间范围
     * @param modeCode 模式
     * @return boolean
     */
    public boolean signOut(Integer modeCode){
        List<DutySchedule> dutySchedules = dutyScheduleService.findOneByModeCode(modeCode);

        for(DutySchedule dutySchedule:dutySchedules){
            DateTime now = DateUtil.date();
            DateTime begin = DateUtil.parse(dutySchedule.getSignOutStart());
            DateTime end = DateUtil.parse(dutySchedule.getSignOutEnd());
            if(DateUtil.isIn(now,begin,end)){
                return true;
            }
        }
        return false;
    }

    public Integer getShiftop(Integer modeCode){
        List<DutySchedule> dutySchedules = dutyScheduleService.findOneByModeCode(modeCode);
        for(DutySchedule dutySchedule:dutySchedules){
            boolean isSign = DateUtil.isIn(DateUtil.date(), DateUtil.parse(dutySchedule.getSignInStart()), DateUtil.parse(dutySchedule.getSignInEnd()));
            if(isSign){
                return dutySchedule.getShiftop();
            }
        }
        return -1;
    }
}
